import { _decorator, Component, director, error, JsonAsset, Layers, Node, Prefab, randomRangeInt, resources, toDegree } from 'cc';
import { Joystick } from './Joystick';
import { Player } from './Player';
import { BattleContext } from './BattleContext';
import { Util } from './Util';
import { Monster } from './Monster';
import { Constant } from './Constant';
import { ResUtil } from './ResUtil';
import { Globals } from './Globals';
import { SkillButton } from './SkillButton';
import { NormalButton } from './NormalButton';
import { ProgressBar } from './ProgressBar';
const { ccclass, property } = _decorator;

@ccclass('Battle')
export class Battle extends Component {
    @property(Node) ndPlayer:Node;
    @property(Node) ndJoystick:Node;
    @property(Node) ndIndicator:Node;
    @property(Node) ndBtnEndGame:Node;
    @property(Node) ndLifeBar:Node; // 主角生命条
    @property(Node) ndExpBar: Node; // 主角经验条
    @property(Node) ndSkillSelectView: Node; // 技能选择面板

    // @property(Node) ndBtnSkill:Node;
    // @property(Node) ndBtnAttack:Node; // 攻击按钮
    // @property(Node) ndBtnFireball:Node; // 火球术按钮
    // @property(Node) ndBtnRoll:Node; // 翻滚按钮

    private _sk0Button: SkillButton;
    private _sk1Button: SkillButton;
    private _sk2Button: SkillButton;
    private _sk3Button: SkillButton;


    protected onLoad(): void {
        BattleContext.ndCamera = this.node.getChildByName('Camera'); // 设置相机节点
        BattleContext.ndPlayer = this.ndPlayer; // 设置玩家节点
        BattleContext.player = this.ndPlayer.getComponent(Player); // 设置玩家组件

        const ndSkills = this.node.getChildByName('Skills');
        console.log('这是什么', ndSkills);
        this._sk0Button = ndSkills.getChildByName('BtnSk0').getComponent(SkillButton);
        this._sk1Button = ndSkills.getChildByName('BtnSk1').getComponent(SkillButton);
        this._sk2Button = ndSkills.getChildByName('BtnSk2').getComponent(SkillButton);
        this._sk3Button = ndSkills.getChildByName('BtnSk3').getComponent(SkillButton);


        // 创建子节点的方法在这里呢
        const createSubNode = (name:string)=> {
            const subNode = new Node(name);
            subNode.layer = Layers.Enum.UI_2D;
            // 在本节点上挂在节点
            this.node.addChild(subNode);
            return subNode;
        }
        // 创建怪物父节点
        BattleContext.ndMonsterParent = createSubNode('MonsterParent');
        // 设置怪物父节点的层级
        BattleContext.ndTextParent = createSubNode('TextParent');
        // 设置武器节点
        BattleContext.ndWeapon = createSubNode('Weapon');

    }

    protected onEnable(): void {
        this._rebindSkillButton();
        this.ndJoystick.getComponent(Joystick).onJoystickEvent((event:number, radian:number|null|undefined)=> {
            switch (event) {
                case Joystick.Event.START:
                    this.ndPlayer.getComponent(Player).isMoving = true;
                    break;
                case Joystick.Event.MOVE:
                    if(radian !==undefined && radian !==null) {
                        this.ndPlayer.getComponent(Player).moveDirection = radian;
                    }
                    break
                case Joystick.Event.END:
                case Joystick.Event.CANCEL:
                    this.ndPlayer.getComponent(Player).isMoving = false;
                    break;
            
                default:
                    break;
            }
        })

        // this.ndBtnSkill.getComponent(SkillButton).onEvent((event:number, radian:number)=> {
        //     switch (event) {
        //         case SkillButton.Event.START:
        //             this.ndIndicator.active = true;
        //             break;

        //         case SkillButton.Event.MOVE:
        //             this.ndIndicator.angle = toDegree(radian);
        //             break;
                
        //         case SkillButton.Event.END:
        //             this.ndIndicator.active = false;
        //             BattleContext.player.castFireball(radian);
        //             break;
        //         case SkillButton.Event.CANCEL:
        //             this.ndIndicator.active = false;
        //             break;
            
        //         default:
        //             break;
        //     }
        // })

        // this.ndBtnFireball.getComponent(SkillButton).onEvent((event:number, radian:number)=> {
        //     switch (event) {
        //         case SkillButton.Event.START:
        //             this.ndIndicator.active = true;
        //             break;

        //         case SkillButton.Event.MOVE:
        //             this.ndIndicator.angle = toDegree(radian);
        //             break;
                
        //         case SkillButton.Event.END:
        //             this.ndIndicator.active = false;
        //             BattleContext.player.castPowerFireballs();
        //             break;
        //         case SkillButton.Event.CANCEL:
        //             this.ndIndicator.active = false;
        //             break;
            
        //         default:
        //             break;
        //     }
        // })

        // this.ndBtnAttack.getComponent(SkillButton).coldDownTime = 0.2;
        // this.ndBtnAttack.getComponent(SkillButton).onEvent((event:number, radian:number)=> {
        //     switch (event) {
        //         case SkillButton.Event.START:
        //             // BattleContext.player.startShootBullet(); // 连续发射

        //             break;

        //         case SkillButton.Event.MOVE:
        //             // BattleContext.player.autoDirection = false; // 关闭自动瞄准
        //             // BattleContext.player.setWeaponAngle(toDegree(radian)); // 设置武器角度
        //             break;
                
        //         case SkillButton.Event.END:
        //             // BattleContext.player.autoDirection = true; // 开启自动瞄准
        //             // BattleContext.player.stopShootBullet(); // 停止射击

        //             BattleContext.player.shootBullet();
        //             break;
        //         case SkillButton.Event.CANCEL:
        //             // BattleContext.player.autoDirection = true; // 开启自动瞄准
        //             // BattleContext.player.stopShootBullet(); // 停止射击
        //             break;
            
        //         default:
        //             break;
        //     }
        // })

        // 
        // this.ndBtnRoll.getComponent(SkillButton).onEvent((event:number, radian: number)=> {
        //     switch (event) {
        //         case SkillButton.Event.END:
        //             // BattleContext.player.castRoll();
        //             BattleContext.player.castUnattackable();
        //             break;
            
        //         default:
        //             break;
        //     }
        // })

        this.ndBtnEndGame.getComponent(NormalButton).onClick(()=> {
            director.loadScene("Main"); // 返回主菜单
        })
        
        // BattleContext
        const LifeBar = this.ndLifeBar.getComponent(ProgressBar);
        LifeBar.setProgress(1);
        const expBar = this.ndExpBar.getComponent(ProgressBar);
        expBar.setProgress(0);
        BattleContext.player.onPlayerEvent((event:number, value:number)=> {
            switch (event) {
                case Player.Event.HURT:
                    LifeBar.setProgress(BattleContext.player.hp / BattleContext.player.maxHp);
                    break;
                case Player.Event.DEAD:
                    // TODO
                    break;
                case Player.Event.ADD_EXP:
                    expBar.setProgress(BattleContext.player.exp / BattleContext.player.maxExp);
                    break;
                case Player.Event.LEVEL_UP:
                    this.ndSkillSelectView.active = true; // 暂时注释
                    break;
            }
        })

        
        this.ndSkillSelectView.active = false;

        this.ndPlayer.getComponent(Player).isMoving = true;
        this.ndIndicator.active = false; // 隐藏指示器
    }

    
    protected onDisable(): void {
        // TODO
    }


    start() {
        // 地图上的装饰添加 TODO 未做
        // 开始游戏 生成敌人
        // this._startGame();

        this.schedule(this._onGameSpawned, 1)
    }

    update(deltaTime: number) {
        
    }

    _rebindSkillButton(){
        this._sk0Button.coldDownTime = 0;
        this._sk0Button.onEvent((event:number,radian:number)=> {
            if(event === SkillButton.Event.END) {
                BattleContext.player.shootBullet();
            }
        })

        this._sk1Button.getComponent(SkillButton).onEvent((event:number,radian:number)=> {
            switch (event) {
                case SkillButton.Event.START:
                    this.ndIndicator.active = true;
                    break;

                case SkillButton.Event.MOVE:
                    this.ndIndicator.angle = toDegree(radian);
                    break;
                
                case SkillButton.Event.END:
                    this.ndIndicator.active = false;
                    BattleContext.player.castPowerFireballs();
                    break;
                case SkillButton.Event.CANCEL:
                    this.ndIndicator.active = false;
                    break;
            
                default:
                    break;
            }
        })

        this._sk2Button.onEvent((event:number,radian:number)=> {
            switch (event) {
                case SkillButton.Event.START:
                    this.ndIndicator.active = true;
                    break;

                case SkillButton.Event.MOVE:
                    this.ndIndicator.angle = toDegree(radian);
                    break;
                
                case SkillButton.Event.END:
                    this.ndIndicator.active = false;
                    BattleContext.player.castFireball(radian);
                    break;
                case SkillButton.Event.CANCEL:
                    this.ndIndicator.active = false;
                    break;
            
                default:
                    break;
            }
        })

        this._sk3Button.onEvent((event:number,radian:number)=> {
            switch (event) {
                case SkillButton.Event.END:
                    BattleContext.player.castUnattackable();
                    break;
            
                default:
                    break;
            }
        })
    }

    private _onGameSpawned() {
        const min = -500;
        const max = 500;

        const createOne = () => {
            const x = randomRangeInt(min, max);
            const y = randomRangeInt(min, max);

            let node = Globals.getNode(Constant.PrefabUrl.PINK_MONSTER, BattleContext.ndMonsterParent);
            node.setPosition(x, y);

            const monster = node.getComponent(Monster);
            monster.speed = 0.6;
            monster.hp = 150;
            monster.onMonsterEvent((event:number)=> {
                switch (event) {
                    case Monster.Event.DEAD:
                        BattleContext.player.addExp(10);
                        break;
                    default:
                        break;
                }
            })
            monster.startAction();
        }

        if(BattleContext.ndMonsterParent.children.length < 3) {
                // 一次刷新x个怪物
                for(let i=0;i<1;i++) {
                    createOne();
                }
        }


        
    }

    private _updatemonsterLayers() {
        // 怪物的层级渲染顺序，随着y值重新排序

        const monsters = BattleContext.ndMonsterParent.children;
        for(let i=0;i<monsters.length;i++) {
            for(let j=0;j<monsters.length;j++) {
                if(monsters[j].position.y > monsters[i].position.y) {
                    const index = monsters[i].getSiblingIndex();
                    monsters[i].setSiblingIndex(index);
                }
            }
        }
    }

    /**
     * 开始游戏
     */
    private _startGame() {
        for (let i = 0; i < 5; i++) {
            let node = Globals.getNode(Constant.PrefabUrl.PINK_MONSTER, BattleContext.ndMonsterParent);
            node.setPosition(randomRangeInt(-1500, 1500), randomRangeInt(-1500, 1500));
            node.getComponent(Monster).speed = 0.5;
            node.getComponent(Monster).hp = 1000;
        }

        // BattleContext.ndPlayer.getComponent(Player).startEndlessDagger(); // 无尽匕首
        // BattleContext.ndPlayer.getComponent(Player).startSurroundingSwords(); // 环绕剑
        // BattleContext.ndPlayer.getComponent(Player).startFireball(); // 火球术 1
    }
    

}


